package admin.controller;

import admin.common.Result;
import admin.entity.User;
import admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xf07b1
 * @wechat xf07b1
 * @description
 */
@RestController
@RequiredArgsConstructor
public class LoginController {

    private final UserService userService;

    /**
     * 登录
     */
    @PostMapping("/api/login")
    public Result login(@RequestBody User user) {
        String token = userService.login(user.getUsername(),user.getPassword());
        return Result.ok().message("登录成功").data(token);
    }

    /**
     * 注册
     */
    @PostMapping("/api/register")
    public Result register(@RequestBody User user) {
        userService.save(user);
        return Result.ok().message("注册成功");
    }

    /**
     * 检验用户名是否存在
     */
    @GetMapping("/api/exist")
    public Result exist(String username) {
        boolean exist = userService.exist(username);
        return Result.ok().data(exist);
    }

    /**
     * 根据用户名获取登录信息
     */
    @GetMapping("/api/info")
    public Result info(String username) {
        User user = userService.getByUsername(username);
        return Result.ok().data(user);
    }
}
